<?php
class cateAction extends baseAction {
	public function index() {
		//var_dump(__APP__);exit;
		//print_r('go');exit;
		$cid = isset($_GET['cid']) && intval($_GET['cid']) ? intval($_GET['cid']) : '';
		$items_mod = D('items');
		$items_cate_mod = D('items_cate');
		import("ORG.Util.Page");
		$sql_where = " 1 ";

		if ($cid) {
			//是否顶级分类
			$cate_info = $items_cate_mod->where('id=' . $cid)->find();
			if ('0' == $cate_info['pid']) {
				//子分类
				$scate = $items_cate_mod->where('pid=' . $cid)->select();
				//查询条件用in
				foreach ($scate as $val) {
					$sid_arr[] = $val['id'];
				}
				$sid_in = implode(',', $sid_arr);
				$sql_where = "cid IN (" . $sid_in . ")";
				$pcid = $cid;
				$this->assign('pcate_info', $cate_info);
				//print_r($cate_info);exit;
			} else {
				//子分类
				$scate = $items_cate_mod->where('pid=' . $cate_info['pid'])->select();
				$pcid = $cate_info['pid'];
				$sql_where = "cid=" . $cid;
				$pcate_info = $items_cate_mod->where('id=' . $cate_info['pid'])->find();
				$this->assign('pcate_info', $pcate_info);
				$this->assign('cate_info', $cate_info);
			}
			$this->assign('scate', $scate);
			$this->seo['seo_title'] = !empty($cate_info['seo_title']) ? $cate_info['seo_title'] : $cate_info['name'];
			$this->seo['seo_title'] = $this->seo['seo_title'] . ' - ' . $this->setting['site_name'];
			$this->seo['seo_keys'] = !empty($cate_info['seo_keys']) ? $cate_info['seo_keys'] : $cate_info['name'];
			!empty($cate_info['seo_desc']) && $this->seo['seo_desc'] = $cate_info['seo_desc'];
		}

		//大分类
		$pcate = $items_cate_mod->where('pid=0')->select();
		$this->assign('pcate', $pcate);

		$p = !empty($_GET['p']) ? intval($_GET['p']) : 1;
		$sp = !empty($_GET['sp']) ? intval($_GET['sp']) : 1;
		$sp >$this->setting['waterfall_sp'] && exit;

		//先计算大的分页
		$list_rows =$this->setting['waterfall_sp']* $this->setting['waterfall_items_num'];
		$s_list_rows =$this->setting['waterfall_items_num'];

		$show_sp = 0;
		$count = $items_mod->where($sql_where)->count();
		$this->assign('count', $count);

		$count > $s_list_rows && $show_sp = 1;
		$pager = new Page($count, $list_rows);
		$page = $pager->show_1();

		$first_row = $pager->firstRow + $s_list_rows * ($sp - 1);
		$items_list = $items_mod->relation(true)->where($sql_where)->limit($first_row . ',' . $s_list_rows)->order('id DESC')->select();
		//print_r( $items_mod->getLastSql());exit;
		//print_r($items_list);exit;
		foreach ($items_list as $key=>$val){
			$items_list[$key]['comments']=$this->items_comments_mod->relation('user')->where('items_id='.$val['id'].' and status=1')
			->order('id desc')->limit('2')->select();
			if($val['status']==0){
				unset($items_list[$key]);
			}
		}

		$this->assign('page', $page);
		$this->assign('p', $p);
		$this->assign('show_sp', $show_sp);
		$this->assign('sp', $sp);
		$this->assign('pcid', $pcid);

		$this->assign('cid', $cid);
		$this->assign('items_list', $items_list);
		//print_r($items_list);exit;
		$this->assign('seo', $this->seo);

		if ($sp > 1) {
			echo($this->fetch('public:goods_list'));
		} else {
			$this->display();
		}
	}
	public function tag() {
		$tag_name = isset($_GET['tag']) && trim($_GET['tag']) ? trim(urldecode($_GET['tag'])) : '';
		$p = !empty($_GET['p']) ? intval($_GET['p']) : 1;
		$sp = !empty($_GET['sp']) ? intval($_GET['sp']) : 1;
		$sp > 5 && exit;
		$list_rows = 200;
		$s_list_rows = 40;
		$show_sp = 0;

		$items_mod = D('items');
		$items_cate_mod = D('items_cate');
		$items_tags_mod = D('items_tags');
		import("ORG.Util.Page");
		$sql_where = "1=1";
		if ($tag_name) {
			$tag = $items_tags_mod->where("name='" . $tag_name . "'")->find();
			$sql_where = 'iti.tag_id=' . $tag['id'];
			$this->assign('tag', $tag);

			$this->seo['seo_title'] = !empty($tag['seo_title']) ? $tag['seo_title'] : $tag['name'];
			$this->seo['seo_title'] = $this->seo['seo_title'] . ' - ' . $this->setting['site_name'];
			$this->seo['seo_keys'] = !empty($tag['seo_keys']) ? $tag['seo_keys'] : $tag['name'];
			!empty($cate_info['seo_desc']) && $this->seo['seo_desc'] = $cate_info['seo_desc'];
		}

		//先计算大的分页
		$count = $items_mod->join("LEFT JOIN " . C('DB_PREFIX') . "items_tags_item as iti ON iti.item_id=" . C('DB_PREFIX') . "items.id")->where($sql_where)->count();
		$count > $s_list_rows && $show_sp = 1;
		$pager = new Page($count, $list_rows);
		$page = $pager->show_1();
		$first_row = $pager->firstRow + $s_list_rows * ($sp - 1);
		$items_list = $items_mod->relation('items_site')->join("LEFT JOIN " . C('DB_PREFIX') . "items_tags_item as iti ON iti.item_id=" . C('DB_PREFIX') . "items.id")->where($sql_where)->limit($first_row . ',' . $s_list_rows)->order('add_time DESC')->select();
		$this->assign('page', $page);
		$this->assign('p', $p);
		$this->assign('show_sp', $show_sp);
		$this->assign('sp', $sp);
		$this->assign('items_list', $items_list);

		//大分类
		$pcate = $items_cate_mod->where('pid=0')->select();
		$this->assign('pcate', $pcate);

		$this->assign('seo', $this->seo);
		$this->display();
	}

	public function like() {
		$id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : '';
		$data = 0;
		if ($id) {
			$items_cate =M('items_cate');
			$data = $items_cate->where('id=' . $id)->setInc('item_likes');
		}
		$this->ajaxReturn($data);
	}
}